package com.ning.activitys.skysound.adpter;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;
import com.ning.activitys.skysound.R;
import com.ning.activitys.skysound.beans.GsonBeansRanking;
import com.ning.activitys.skysound.volley.VolleySingleton;

import java.util.ArrayList;

/**
 * Created by dllo on 16/1/17.
 */
public class RankingAdapter extends BaseAdapter{

    private Context context;
    private GsonBeansRanking gsonBeansRanking;

    public RankingAdapter(Context context,GsonBeansRanking gsonBeansRanking){
        this.context = context;
        this.gsonBeansRanking = gsonBeansRanking;

    }

    @Override
    public int getCount() {
        return gsonBeansRanking.getGroups().get(0).getRanks().size();
    }

    @Override
    public Object getItem(int position) {
        return gsonBeansRanking.getGroups().get(0).getRanks().get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder myViewHolder;
        if (convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.item_ranking_list,parent,false);
            myViewHolder = new MyViewHolder(convertView);

            convertView.setBackgroundColor(Color.parseColor("#FFFFFF"));

            convertView.setTag(myViewHolder);
        }else {
            myViewHolder = (MyViewHolder) convertView.getTag();
        }
        myViewHolder.title.setText(gsonBeansRanking.getGroups().get(0).getRanks().get(position).getTitle());
        myViewHolder.song1.setText(gsonBeansRanking.getGroups().get(0).getRanks().get(position).getRankSongs().get(0).getTitle());
        myViewHolder.song2.setText(gsonBeansRanking.getGroups().get(0).getRanks().get(position).getRankSongs().get(1).getTitle());
        myViewHolder.song3.setText(gsonBeansRanking.getGroups().get(0).getRanks().get(position).getRankSongs().get(2).getTitle());

        VolleySingleton.getInstance().loadImage(myViewHolder.img,
                gsonBeansRanking.getGroups().get(0).getRanks().get(position).getImg());

        return convertView;
    }

    class MyViewHolder {

        private TextView title,song1,song2,song3;
        private NetworkImageView img;

        public MyViewHolder(View view){

            img = (NetworkImageView) view.findViewById(R.id.iv_image_ranking);
            title = (TextView) view.findViewById(R.id.tv_title_ranking);
            song1 = (TextView) view.findViewById(R.id.tv_song1_ranking);
            song2 = (TextView) view.findViewById(R.id.tv_song2_ranking);
            song3 = (TextView) view.findViewById(R.id.tv_song3_ranking);
        }
    }
}
